package com.example.ymm.modules1.controller.sys;

import com.example.ymm.model.CommonResult;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.controller.BaseController;
import com.example.ymm.modules1.entity.my.user.DbRewards;
import com.example.ymm.modules1.entity.my.user.DbUserRewards;
import com.example.ymm.modules1.query.DbRewardsQueryCriteria;
import com.example.ymm.modules1.query.DbUserRewardsQueryCriteria;
import com.example.ymm.modules1.service.dto.ImportUserRewardsExcelDto;
import com.example.ymm.modules1.service.user.DbRewardsService;
import com.example.ymm.modules1.service.user.DbUserRewardsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/hl/sys/rewards")
@Api(tags = "pc 奖惩管理")
@Slf4j
@CrossOrigin
public class SysRewardsController  extends BaseController {

    @Autowired
    DbUserRewardsService dbUserRewardsService;
    @Autowired
    DbRewardsService dbRewardsService;


    @ApiOperation(value = "奖惩规则列表")
    @RequestMapping(value = "/rewardsList", method = RequestMethod.POST)
    public CommonResult rewardsList(@Valid DbRewardsQueryCriteria criteria, YmPageDto ymPage) {
        try{
            TableList<DbRewards> list = dbRewardsService.list(criteria, ymPage);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "奖惩规则下拉列表")
    @RequestMapping(value = "/rewardsSelectList", method = RequestMethod.POST)
    public CommonResult rewardsSelectList(@Valid DbRewardsQueryCriteria criteria) {
        try{
            List<DbRewards> dbRewards = dbRewardsService.queryAll(criteria);
            return CommonResult.success(dbRewards,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "奖惩规则保存")
    @RequestMapping(value = "/saveRewards", method = RequestMethod.POST)
    public CommonResult saveRewards(DbRewards rewards) {
        try{
            dbRewardsService.saveOrUpdate(rewards);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "奖惩规则详情")
    @RequestMapping(value = "/rewardsDetail", method = RequestMethod.POST)
    public CommonResult rewardsDetail(@ApiParam(value = "数据主键") @RequestParam(required = true) String rewardsId) {
        try{
            DbRewards rewards = dbRewardsService.getById(rewardsId);
            return CommonResult.success(rewards);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "奖惩规则删除")
    @RequestMapping(value = "/rewardsDelete", method = RequestMethod.POST)
    public CommonResult rewardsDelete(@ApiParam(value = "数据主键") @RequestParam(required = true) String rewardsId) {
        try{
            dbRewardsService.removeById(rewardsId);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "用户奖惩导入")
    @RequestMapping(value = "/importUserRewardsExcel", method = RequestMethod.POST)
    public CommonResult importUserRewardsExcel(@RequestBody ImportUserRewardsExcelDto dto) {
        try{
            List<DbUserRewards> list=dto.getList();
            dbUserRewardsService.importUserRewardsExcel(list);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "用户奖惩记录列表")
    @RequestMapping(value = "/userRewardsList", method = RequestMethod.POST)
    public CommonResult userRewardsList(@Valid DbUserRewardsQueryCriteria criteria, YmPageDto ymPage) {
        try{
            TableList<DbUserRewards> list = dbUserRewardsService.list(criteria, ymPage);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "用户奖惩记录保存")
    @RequestMapping(value = "/saveUserRewards", method = RequestMethod.POST)
    public CommonResult saveUserRewards(DbUserRewards userRewards) {
        try{
            dbUserRewardsService.addToUser(userRewards);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "奖惩规则详情")
    @RequestMapping(value = "/userRewardsDetail", method = RequestMethod.POST)
    public CommonResult userRewardsDetail(@ApiParam(value = "数据主键") @RequestParam(required = true) String userRewardsId) {
        try{
            DbUserRewards userRewards = dbUserRewardsService.getById(userRewardsId);
            return CommonResult.success(userRewards);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

}
